<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/9/29
 * Time: 11:59
 */

namespace app\admin\controller;

use app\common\model\AdminModel;
use app\common\model\RoleAuthModel;
use app\common\model\RoleModel;

class RoleController   extends Base
{
    private $model;

    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->model = new RoleModel();
    }
    /**
     * 列表
     * @return array|string
     * @throws \Exception
     */
    public function index()
    {
        $pageSize = $this->request->param('pageSize/d', 20); //每页多少条
        $pageNumber = $this->request->param('pageNumber/d',1); //当前页
        $keyword = $this->request->param('keyword');
        $map = [];
        if (!empty($keyword)) {
            $map[] = ['name', 'like', '%' . $keyword . '%'];
        }

        $list = $this->model
            ->where($map)
            ->order('id','asc')
            ->paginate($pageSize, false, ['page' => $pageNumber]);
        $this->result($list, '操作成功', 1);
    }
    //小程序所有功能按键
    public function getSmallColumnListByRoleId()
    {
        $role_id = $this->request->param('role_id/d');
        $list = [];
        $roleAuthList = RoleAuthModel::where('status',1)->select();
        $customer_auth =[];
        $customer_goods_auth =[];
        $order_goods_auth =[];

        $roleInfo = $this->model->where('id',$role_id)->where('status',1)->field('customer_auth,customer_goods_auth,order_goods_auth')->find()->toArray();
        foreach($roleAuthList as &$v){
            $v['is_check'] = 0;
            if($v['type_id'] == 1){
                if($roleInfo['customer_auth'] && in_array($v['id'],explode(',',$roleInfo['customer_auth']))){
                    $v['is_check'] = 1;
                }
                $customer_auth[]  = $v;
            }elseif($v['type_id'] == 2){
                if($roleInfo['customer_goods_auth'] && in_array($v['id'],explode(',',$roleInfo['customer_goods_auth']))){
                    $v['is_check'] = 1;
                }
                $customer_goods_auth[] = $v;
            }elseif($v['type_id'] == 3){
                if($roleInfo['order_goods_auth'] && in_array($v['id'],explode(',',$roleInfo['order_goods_auth']))){
                    $v['is_check'] = 1;
                }
                $order_goods_auth[] = $v;
            }
        }
        $list['customer_auth'] = $customer_auth;
        $list['customer_goods_auth'] = $customer_goods_auth;
        $list['order_goods_auth'] = $order_goods_auth;

        $this->result($list, '操作成功', 1);

    }
    //配置小程序功能权限
    public function roleAuthByRoleId()
    {
        $role_id = $this->request->param('role_id/d');
        $customer_auth = $this->request->param('customer_auth');
        $customer_goods_auth = $this->request->param('customer_goods_auth');
        $order_goods_auth = $this->request->param('order_goods_auth');

        $result =  $this->model->where('id',$role_id)->update(['customer_auth'=>$customer_auth,'customer_goods_auth'=>$customer_goods_auth,'order_goods_auth'=>$order_goods_auth]);
        $this->result($result, '操作成功', 1);
    }
}